/* Copyright 2007 Gearbox Software - http://www.gearboxsoft.com
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http:www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/
/*
 * Created by: 
 * Created: Monday, June 18, 2007
 */

namespace GearBlog.UnitTests
{
	using System.Collections.Generic;
	using System.Configuration;
	using System.Reflection;
	using Castle.ActiveRecord;
	using Castle.ActiveRecord.Framework;
	using Castle.MonoRail.TestSupport;
	using GearBlog.Web.Controllers;
	using GearBlog.Web.Models;
	using NHibernate.Expression;
	using NUnit.Framework;

	[TestFixture]
	public class BlogPostControllerTestCase:BaseControllerTest
	{
		private BlogPostController controller;

		[TestFixtureSetUp]
		public void FixtureSetup()
		{
			ActiveRecordService.Start();

		}

		[TestFixtureTearDown]
		public void FixtureTearDown()
		{
			ActiveRecordService.Stop();
		}
		/// <summary>
		/// Setup controller for testing
		/// We are not using SetUp method because we want the test to be able to control the action it calls
		/// </summary>
		private void SetupController(string action)
		{
			controller=new BlogPostController();
			PrepareController(controller,"","BlogPost",action);
			Context.CurrentUser = ActiveRecordService.CurrentUser;

		}
		[Test]
		public void TestPost()
		{
			SetupController("ShowAddPost");
			
			controller.ShowAddPost();

			CreateAndSavePost();

			Assert.AreEqual("Post saved successfully.", controller.Flash["message"]);

			Post post = Post.FindOne(new EqExpression("Title", "Test Post"));
			Assert.AreNotEqual(0, post.Id,"Unable to find saved post");

			//clean up
			List<ActiveRecordBase> deleteList=new List<ActiveRecordBase>();
			deleteList.Add(post);
			foreach (Tag tag in post.Tags)
			{
				deleteList.Add(tag);
			}

			foreach (ActiveRecordBase recordBase in deleteList)
			{
				recordBase.Delete();
			}
		}

		private void CreateAndSavePost()
		{

			SetupController("SavePost");
			//create post
			Post post=new Post();
			post.Title = "Test Post";
			post.Body = "Hello World";
			post.TagsString = "tag1, tag2";

			controller.SavePost(post);
		}

		[Test]
		public void TestEditPost()
		{
			CreateAndSavePost();

			Post post = Post.FindOne(new EqExpression("Title", "Test Post"));
			
			SetupController("ShowEditPost");

			controller.ShowEditPost(post.Id);

			post = (Post) controller.PropertyBag["PostForm"];

			post.Body = "Hello World Changed";

			SetupController("SavePost");
			
			controller.SavePost(post);

			post=Post.FindOne(new EqExpression("Title", "Test Post"));

			Assert.AreEqual("Hello World Changed", post.Body);
			
		}
	}
}